package com.huabo.contract.service.impl;

import com.hbfk.util.PageInfo;
import com.huabo.contract.config.DateBaseConfig;
import com.huabo.contract.oracle.entity.TblAttachment;
import com.huabo.contract.oracle.entity.TblDispuinfo;
import com.huabo.contract.oracle.entity.TblLegalDisputregistration;
import com.huabo.contract.oracle.mapper.TblLegalDisputregistrationMapper;
import com.huabo.contract.service.TblLegalDisputregistrationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TblLegalDisputregistrationServiceImpl implements TblLegalDisputregistrationService {


    @Resource
    private TblLegalDisputregistrationMapper tblLegalDisputregistrationMapper;

    @Override
    public TblLegalDisputregistration findById(Integer disputeid) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return tblLegalDisputregistrationMapper.selectDisputeId(disputeid);
        } else {
            return tblLegalDisputregistrationMapper.selectDisputeId(disputeid);
        }
    }

    @Override
    public void findListByPageInfo(PageInfo<TblLegalDisputregistration> pageInfo, TblLegalDisputregistration dispute, BigDecimal pid) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            pageInfo.setTlist(tblLegalDisputregistrationMapper.findListByPageInfo(pageInfo, dispute, pid));
            pageInfo.setTotalRecord(tblLegalDisputregistrationMapper.findListByPageInfoCount(dispute, pid));

            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("pageInfo", pageInfo);
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            pageInfo.setTlist(tblLegalDisputregistrationMapper.findListByPageInfo(pageInfo, dispute, pid));
            pageInfo.setTotalRecord(tblLegalDisputregistrationMapper.findListByPageInfoCount(dispute, pid));

            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("pageInfo", pageInfo);
        }
    }

    @Override
    public void addDiputregistration(TblLegalDisputregistration dispute) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
//        if (attids != null && !"".equals(attids)) {
//            List<TblAttachment> attList = new ArrayList(0);
//            String[] attid = attids.split(",");
//            TblAttachment att = null;
//            String[] var6 = attid;
//            int var7 = attid.length;
//
//            for(int var8 = 0; var8 < var7; ++var8) {
//                String id = var6[var8];
//                att = new TblAttachment();
//                att.setAttid(new BigDecimal(id));
//                attList.add(att);
//            }
//
//            dispute.setAttList(attList);
//        }

            this.tblLegalDisputregistrationMapper.saveDiputregistration(dispute);
        } else {
            this.tblLegalDisputregistrationMapper.saveDiputregistration(dispute);
        }
    }

    @Override
    public void deleteRelation(String attid) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            tblLegalDisputregistrationMapper.deleteRelation(attid);
        } else {
            tblLegalDisputregistrationMapper.deleteRelation(attid);
        }
    }

    @Override
    public void removecaseInformation(Integer disputeId) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            tblLegalDisputregistrationMapper.removecaseInformation(disputeId);
        } else {
            tblLegalDisputregistrationMapper.removecaseInformation(disputeId);
        }
    }

    @Override
    public TblLegalDisputregistration findByDisputeId(Integer disputeId) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return tblLegalDisputregistrationMapper.findByDisputeId(disputeId);
        } else {
            return tblLegalDisputregistrationMapper.findByDisputeId(disputeId);
        }
    }

    @Override
    public void findListByPageInfoDispute(PageInfo<TblLegalDisputregistration> pageInfo, TblLegalDisputregistration dispute,BigDecimal pid,Integer choiceType,BigDecimal oid) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);

            //纠纷结案 查询纠纷信息
            String disputeIds = null;
            List<TblDispuinfo> list = null;
            if (choiceType == 1) {
                list = this.tblLegalDisputregistrationMapper.findDisputeIdsForDispute("DISPUTINFO AS DISPUINFO", "TBL_LEGAL_CLOSEINFORMATION", "LINKORG", "", oid);
            } else if (choiceType == 0) {
                //协商过程选择纠纷信息
                list = this.tblLegalDisputregistrationMapper.findDisputeIdsForDispute("DISPUINFO", "TBL_LEGAL_NEGOTIATEDSETTLEMEN", "LINKORG", "", oid);
            } else if (choiceType == 2) {
                //诉讼过程 选择纠纷信息 剔除协商一致 的记录
                list = this.tblLegalDisputregistrationMapper.findDisputeIdsForDispute("DISPUINFO", "TBL_LEGAL_NEGOTIATEDSETTLEMEN", "LINKORG", " AND ISAGGREE = 1", oid);
            }
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    TblDispuinfo tblDispuinfo = list.get(i);
                    if (tblDispuinfo != null) {
                        if (i == 0) {
                            disputeIds = tblDispuinfo.getDISPUINFO();
                        } else {
                            disputeIds += ',' + tblDispuinfo.getDISPUINFO();
                        }
                    }

                }
            }

            pageInfo.setTlist(tblLegalDisputregistrationMapper.findListByPageInfoDispute(pageInfo, dispute, pid, disputeIds));
            pageInfo.setTotalRecord(tblLegalDisputregistrationMapper.findListByPageInfoDisputeCount(dispute, pid, disputeIds));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("date", pageInfo);
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);

            //纠纷结案 查询纠纷信息
            String disputeIds = null;
            List<TblDispuinfo> list = null;
            if (choiceType == 1) {
                list = this.tblLegalDisputregistrationMapper.findDisputeIdsForDispute("DISPUTINFO AS DISPUINFO", "TBL_LEGAL_CLOSEINFORMATION", "LINKORG", "", oid);
            } else if (choiceType == 0) {
                //协商过程选择纠纷信息
                list = this.tblLegalDisputregistrationMapper.findDisputeIdsForDispute("DISPUINFO", "TBL_LEGAL_NEGOTIATEDSETTLEMEN", "LINKORG", "", oid);
            } else if (choiceType == 2) {
                //诉讼过程 选择纠纷信息 剔除协商一致 的记录
                list = this.tblLegalDisputregistrationMapper.findDisputeIdsForDispute("DISPUINFO", "TBL_LEGAL_NEGOTIATEDSETTLEMEN", "LINKORG", " AND ISAGGREE = 1", oid);
            }
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    TblDispuinfo tblDispuinfo = list.get(i);
                    if (tblDispuinfo != null) {
                        if (i == 0) {
                            disputeIds = tblDispuinfo.getDISPUINFO();
                        } else {
                            disputeIds += ',' + tblDispuinfo.getDISPUINFO();
                        }
                    }

                }
            }

            pageInfo.setTlist(tblLegalDisputregistrationMapper.findListByPageInfoDispute(pageInfo, dispute, pid, disputeIds));
            pageInfo.setTotalRecord(tblLegalDisputregistrationMapper.findListByPageInfoDisputeCount(dispute, pid, disputeIds));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("date", pageInfo);
        }
    }

    @Override
    public void findListByDispute(PageInfo<TblLegalDisputregistration> pageInfo, TblLegalDisputregistration dispute) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            pageInfo.setTlist(tblLegalDisputregistrationMapper.findListByDispute(pageInfo, dispute));
            pageInfo.setTotalRecord(tblLegalDisputregistrationMapper.findListByDisputeCount(dispute));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("date", pageInfo);
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            pageInfo.setTlist(tblLegalDisputregistrationMapper.findListByDispute(pageInfo, dispute));
            pageInfo.setTotalRecord(tblLegalDisputregistrationMapper.findListByDisputeCount(dispute));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("date", pageInfo);
        }
    }

    @Override
    public TblLegalDisputregistration findDisputeId(Integer disputeId) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return tblLegalDisputregistrationMapper.findDisputeId(disputeId);
        } else {
            return tblLegalDisputregistrationMapper.findDisputeId(disputeId);
        }
    }

    @Override
    public TblLegalDisputregistration findByDidputeid(Integer disputeId) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return tblLegalDisputregistrationMapper.findDisputeId(disputeId);
        } else {
            return tblLegalDisputregistrationMapper.findDisputeId(disputeId);
        }
    }

    @Override
    public void saveDiputregistration(TblLegalDisputregistration stration) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            tblLegalDisputregistrationMapper.saveDiputregistration(stration);
        } else {
            tblLegalDisputregistrationMapper.saveDiputregistration(stration);
        }
    }

    @Override
    public void updateDiputregistration(TblLegalDisputregistration stration) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            tblLegalDisputregistrationMapper.updateDiputregistration(stration);
        } else {
            tblLegalDisputregistrationMapper.updateDiputregistration(stration);
        }
    }

    @Override
    public void modifyDiputregistration(TblLegalDisputregistration oldDispute) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            tblLegalDisputregistrationMapper.updateDiputregistration(oldDispute);
        } else {
            tblLegalDisputregistrationMapper.updateDiputregistration(oldDispute);
        }
    }

    @Override
    public TblLegalDisputregistration findBydisputeId(Integer disputeId) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return tblLegalDisputregistrationMapper.findBydisputeId(disputeId);
        } else {
            return tblLegalDisputregistrationMapper.findBydisputeId(disputeId);
        }
    }

    @Override
    public TblLegalDisputregistration findByOrgid(Integer disputeId, BigDecimal orgid) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return tblLegalDisputregistrationMapper.findByOrgid(disputeId, orgid);
        } else {
            return tblLegalDisputregistrationMapper.findByOrgid(disputeId, orgid);
        }
    }

    @Override
    public void saveBidType(Integer type, Integer bid,BigDecimal aid) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            tblLegalDisputregistrationMapper.saveAttacheMent(type, bid, aid);
        } else {
            tblLegalDisputregistrationMapper.saveAttacheMent(type, bid, aid);
        }
    }

    @Override
    public Map<String, Object> findAttacheMentByBid(Integer type, Integer bid) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            List<TblAttachment> list = tblLegalDisputregistrationMapper.findAttacheMentByBid(type, bid);
            resultMap.put("code", "1");
            resultMap.put("msg", "成功");
            resultMap.put("data", list);
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            List<TblAttachment> list = tblLegalDisputregistrationMapper.findAttacheMentByBid(type, bid);
            resultMap.put("code", "1");
            resultMap.put("msg", "成功");
            resultMap.put("data", list);
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> deleteAttacheMentByBid(Integer type, Integer aid) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            this.tblLegalDisputregistrationMapper.deleteAttacheMentByBid(type, aid);
            resultMap.put("code", "1");
            resultMap.put("msg", "成功");
            resultMap.put("data", null);
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            this.tblLegalDisputregistrationMapper.deleteAttacheMentByBid(type, aid);
            resultMap.put("code", "1");
            resultMap.put("msg", "成功");
            resultMap.put("data", null);
            return resultMap;
        }
    }

    @Override
    public void deleteAttacheMents(Integer type, BigDecimal bid) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            this.tblLegalDisputregistrationMapper.deleteAttacheMents(type, bid);
        } else {
            this.tblLegalDisputregistrationMapper.deleteAttacheMents(type, bid);
        }
    }

//    @Override
//    public TblLegalCloseinformation selectCloseId(Integer closeid) {
//
//        return tblLegalDisputregistrationMapper.selectCloseid(closeid);
//    }


}
